package com.zsrt.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Properties;

import com.zsrt.common.constant.DBConstant;
import com.zsrt.common.entity.DBEntity;

/**
 * 跟数据库相关的工具类
 * 
 * @author zsrt
 *
 */
public class DBUtil {

	/**
	 * 关闭资源
	 * 
	 * @param ps
	 * @param connection
	 */
	public static void closeResources(PreparedStatement ps, Connection connection) {
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (null != connection) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}

	public static void closeResources(ResultSet rs, PreparedStatement ps, Connection connection) {

		if (null != rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (null != connection) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}

	/**
	 * 获得对应数据库的连接
	 * 
	 * @return
	 */
	public static Connection getConnection() {

		//DBEntity entity = initConfig(DBConstant.LOCATION);
		DBEntity entity = readConfig();
		System.out.println(entity);

		String driver = entity.getDriver();

		Connection connection = null;
		try {
			// 1 加载驱动类 ?
			Class.forName(driver);

			// 2 获得连接
			connection = DriverManager.getConnection(entity.getUrl(), entity.getUser(), entity.getPassword());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
	
	
	public static DBEntity readConfig(){
		Properties prop = new Properties();     
		InputStream is = DBUtil.class.getClassLoader().getResourceAsStream(DBConstant.DB_CONFIG_LOCATION);
		DBEntity entity = null;
		try {
			prop.load(is);
			Iterator<String> it=prop.stringPropertyNames().iterator();
			entity = new DBEntity();
			while(it.hasNext()){
				String key=it.next();
				String value = prop.getProperty(key);
				if (DBConstant.DRIVER.equals(key)) {
					entity.setDriver(value);
				} else if (DBConstant.URL.equals(key)) {
					entity.setUrl(value);
				} else if (DBConstant.USER.equals(key)) {
					entity.setUser(value);
				} else if (DBConstant.PASSWORD.equals(key)) {
					entity.setPassword(value);
				}
				System.out.println(key+":"+prop.getProperty(key));
			}
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}     
		return entity;
	            
	}

	/**
	 * 读取制定的配置文件 返回连接数据库的4个关键因素
	 * 
	 * @param location
	 * @return
	 */
	public static DBEntity initConfig(String location) {
		// 侦察兵 经验
		File file = new File(location);

		DBEntity entity = null;
		BufferedReader br = null;
		Reader reader = null;
		try {
			// 大部队
			reader = new FileReader(file);

			// 集团军
			br = new BufferedReader(reader);

			String jieguo = "";

			entity = new DBEntity();

			while ((jieguo = br.readLine()) != null) {
				if (null != jieguo && jieguo.contains("=")) {
					String[] dbs = jieguo.split("=");
					String key = dbs[0];
					String value = dbs[1];
					if (DBConstant.DRIVER.equals(key)) {
						entity.setDriver(value);
					} else if (DBConstant.URL.equals(key)) {
						entity.setUrl(value);
					} else if (DBConstant.USER.equals(key)) {
						entity.setUser(value);
					} else if (DBConstant.PASSWORD.equals(key)) {
						entity.setPassword(value);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != br) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (null != reader) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return entity;
	}

}
